<template>
  <div>
    <el-select
      ref="selectTreeRef"
      clearable
      multiple
      v-model="selectOption"
      placeholder="请选择">
      <el-option>
<!--        <el-input
          placeholder="搜索图标"
          suffix-icon="Search"
        />-->
        <div id="option-body">
          <el-tabs v-model="activeTabName">
            <el-tab-pane name="baseIcon">
              <template #label>
                基本图标
              </template>
              <el-row :gutter="5" justify="start" align="middle" style="width:300px;height: 100%;">
                <el-col :span="4" v-for="(icon) in iconList" :key="icon" class="grid-col">
                  <el-tooltip :content="icon.name" placement="bottom" effect="light" :offset="0" :hide-after="0" :show-after="500">
                    <el-card shadow="hover" @mouseover="changeIconSize(icon,iconSizeOfBig)" @mouseout="changeIconSize(icon,iconSizeOfNormal)">
                      <el-icon :size="icon.iconSize"><component class="icon" :is="icon.name" /></el-icon>
                    </el-card>
                  </el-tooltip>
                </el-col>
              </el-row>
            </el-tab-pane>
          </el-tabs>
          <div id="search-input">
            <el-input
              placeholder="搜索图标"
              suffix-icon="Search"
            />
          </div>
        </div>
      </el-option>
    </el-select>
  </div>
</template>

<script lang="ts" setup>
// 图标选择列表
import { ref, defineComponent, onMounted } from 'vue';
import * as Icons from '@element-plus/icons-vue';

const selectOption = ref();
const iconSizeOfBig = 32; // 大图标大小
const iconSizeOfNormal = 20; // 图标正常大小

// 定义图像类
class Icon {
  name: string;

  iconSize: number;

  constructor(name: string, size: number) {
    this.name = name;
    this.iconSize = size;
  }
}
// 定义图标列表
const iconList = ref<Icon[]>([]);
// 组件初始化时获取图标对象，后期待优化
onMounted(() => {
  // 获取element plus的图标名称
  Object.keys(Icons).forEach((key) => {
    iconList.value.push(new Icon(key, iconSizeOfNormal));
  });
});
// 图标变化
const changeIconSize = (icon:Icon, size:number) => { // 第一个参数：图像对象；第二个参数：要改变的图标大小
  // eslint-disable-next-line no-param-reassign
  icon.iconSize = size;
};
const activeTabName = ref('baseIcon');
</script>

<style lang="scss" scoped>
#option-body{ /*下拉框主体部分*/
  position: relative;
  #search-input{ /*搜索文本框*/
    position: absolute;
    width:120px;
    right:10px;
    top:1px;
  }
  .grid-col{
    height:50px;
    padding:2px;
    /*设置水平垂直居中*/
    display:flex;
    justify-content: center;
    align-items: center;
    /*设置边框*/
    //border: 1px solid #ededed;
    color: #606266;
    /*text-align:center;*/
    font-size:12px;
    ::v-deep .el-card{
      width:100%;
      height:100%;
      /*设置水平垂直居中，这样el-card__body才能居中*/
      display:flex;
      justify-content: center;
      align-items: center;
      .el-card__body{
        /*设置水平垂直居中,这样里面的图标才能居中*/
        display:flex;
        justify-content: center;
        align-items: center;
      }
    }
  }
}
.el-select-dropdown__item{  /*修改下拉菜单的样式*/
  height:auto;/*修改下拉框的高度为自动，否则只显示一个option的高度*/
  padding:0;/*因为option默认是有padding的，这样tree无法扩充到整个option的宽度*/
  padding:0 3px;
  background-color: #ffffff;
  font-weight: normal;/*在封装为组件后，font-wight会变为700数值，导致菜单字体加粗，这里特意设置恢复为原来的字体*/
}
</style>
